package com.swanclouds.redfish.identity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.swanclouds.model.identity.AuthStore;
import com.swanclouds.model.identity.builder.AuthStoreBuilder;
import com.swanclouds.redfish.common.Auth;

public class RedfishAuth implements AuthStore {

    @JsonProperty("UserName")
    private String username;

    @JsonProperty("Password")
    private String password;

    @JsonIgnore
    private String domain;

    @JsonIgnore
    private transient Auth.Type type;

    public RedfishAuth() {
        super();
    }

    public RedfishAuth(String tokenId) {
        this.type = Auth.Type.TOKEN;
    }

    public RedfishAuth(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public RedfishAuth(String username, String password, Auth.Type type) {
        this.username = username;
        this.password = password;
        this.type = type;
    }


    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getDomain() {
        return this.domain;
    }

    protected RedfishAuth(Auth.Type type) {
        this.type = type;
    }

    public Auth.Type getType() {
        return this.type;
    }

    @Override
    public String toString() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
        }
        return "RedfishAuth {" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", domain='" + domain + '\'' +
                ", type=" + type +
                '}';
    }

    @Override
    public AuthStoreBuilder toBuilder() {
        return new RedfishAuthSoreBuilder();
    }

    public static AuthStoreBuilder builder() {
        return new RedfishAuth().toBuilder();
    }

    public static class RedfishAuthSoreBuilder implements AuthStoreBuilder {

        public RedfishAuth m;

        public RedfishAuthSoreBuilder() {
            this(new RedfishAuth());
        }

        public RedfishAuthSoreBuilder(RedfishAuth m) {
            this.m = m;
        }

        @Override
        public AuthStoreBuilder UserName(String userName) {
            m.username = userName;
            return this;
        }

        @Override
        public AuthStoreBuilder Password(String password) {
            m.password = password;
            return this;
        }

        /**
         * Creates and return the Model Entity M
         *
         * @return M instance
         */
        @Override
        public AuthStore build() {
            return m;
        }

        /**
         * Creates a Builder using the param M as the default values
         *
         * @param in The Model M
         * @return Builder
         */
        @Override
        public AuthStoreBuilder from(AuthStore in) {
            m = (RedfishAuth) in;
            return this;
        }

        @Override
        public String toString() {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                return objectMapper.writeValueAsString(this);
            } catch (JsonProcessingException e) {
            }
            return "RedfishAuthSoreBuilder {" +
                    "m=" + m +
                    '}';
        }
    }
}